//
//  XGHomeViewController.m
//  Instagram
//
//  Created by monkey on 2019/4/13.
//  Copyright © 2019 itcast. All rights reserved.
//

#import "XGHomeViewController.h"
#import "XGUserListTableViewController.h"
#import "XGEditViewController.h"

#import "XGCollectionHeaderView.h"

/// headerView重用标识符
static NSString *const XGCollectionHeaderViewReuseIdentifier = @"XGCollectionHeaderView";

@interface XGHomeViewController () <XGCollectionHeaderViewDelegate>

@end

@implementation XGHomeViewController


// 重写默认构造方法防止父类崩溃 首页可以使用默认构造方法
- (instancetype)init
{
    if (self = [super initWithUserInfo:[XGAccountViewModel currentUser]]) {
    }
    
    return self;
}

#pragma mark - 控制器生命周期方法

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 设置导航栏
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"推荐用户" style:UIBarButtonItemStylePlain target:self action:@selector(showRecommendUserListAction)];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    NSIndexSet *set = [[NSIndexSet alloc] initWithIndex:0];
    [self.collectionView reloadSections:set];
}

#pragma mark - 事件监听

/// 展示推荐用户列表事件
- (void)showRecommendUserListAction
{
    XGUserListTableViewController *viewController = [XGUserListTableViewController viewControllerWithUserInfo:[XGAccountViewModel currentUser] listType:XGUserListTypeRecommend];
    [self.navigationController pushViewController:viewController animated:YES];
}

#pragma mark - 其他方法

/// 设置collectionView
- (void)setUpCollectionView
{
    [super setUpCollectionView];

    // 计算header头部
    UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout;
    CGFloat height = 20 + 80 + 10;
    height += [[XGAccountViewModel currentUser].screenName sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:20]}].height + 10;
    height += [self.user[@"descriptionInfo"] boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 40, MAXFLOAT) options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:15]} context:nil].size.height + 5;
    height = ceil(height);
    flowLayout.headerReferenceSize = CGSizeMake(self.view.width, height);
    
    // 注册headerView
    [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([XGCollectionHeaderView class]) bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:XGCollectionHeaderViewReuseIdentifier];
}

#pragma mark - UICollectionViewDelegate

// 返回headerView
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        XGCollectionHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:XGCollectionHeaderViewReuseIdentifier forIndexPath:indexPath];
        // 设置用户数据
        headerView.user = self.user;
        // 设置代理
        headerView.delegate = self;        
        return headerView;
    }
    
    return nil;
}

#pragma mark - XGCollectionHeaderViewDelegate

/// 点击headerView帖子数代理方法
- (void)collectionHeaderViewTopicCountDidClick:(XGCollectionHeaderView *)headerView
{
    if (self.topicList.count > 0) {
        [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] atScrollPosition:UICollectionViewScrollPositionTop animated:YES];
    }
}

/// 点击headerView粉丝数代理方法
- (void)collectionHeaderViewFansCountDidClick:(XGCollectionHeaderView *)headerView
{
    XGUserListTableViewController *viewController = [XGUserListTableViewController viewControllerWithUserInfo:self.user listType:XGUserListTypeFollower];
    [self.navigationController pushViewController:viewController animated:YES];
}

/// 点击headerView关注数代理方法
- (void)collectionHeaderViewFocusCountDidClick:(XGCollectionHeaderView *)headerView
{
    XGUserListTableViewController *viewController = [XGUserListTableViewController viewControllerWithUserInfo:self.user listType:XGUserListTypeFollowee];
    [self.navigationController pushViewController:viewController animated:YES];
}

/// 点击headerView按钮代理方法
- (void)collectionHeaderViewEditOrFocusDidClick:(XGCollectionHeaderView *)headerView button:(UIButton *)button
{
    XGEditViewController *viewController = [[XGEditViewController alloc] init];
    [self.navigationController pushViewController:viewController animated:YES];
}

@end
